<template>
  <router-link :to="{ path: '/Images' }" class="nav-link">/自动化测试截图</router-link>
  <router-link :to="{ path: '/MobileTodoView' }" class="nav-link">/移动端</router-link>
  <router-link :to="{ path: '/CourseGrades' }" class="nav-link">/课程成绩</router-link>
  <router-link :to="{ path: '/PersonalGrades' }" class="nav-link">/个人成绩</router-link>


  <div class="todo-container">
    <el-header class="top-bar">
      <el-button @click="handleLogout" type="danger" class="logout-btn">退出登录</el-button>
    </el-header>

    <el-main>
      <h1>Todo List</h1>
      <TodoInput @add-todo="addTodo" />
      <ul>
        <TodoItem v-for="(todo, index) in todos" :key="index" :todo="todo" :index="index" @remove-todo="removeTodo"
          @update-todo="updateTodo" />
      </ul>
      <p v-if="remainingTodos === 0">暂无任务</p>
      <p v-else>剩余 {{ remainingTodos }} 项任务。</p>
      <TodoClearButton @clear-todos="clearTodos" />
    </el-main>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import TodoInput from './TodoInput.vue';
import TodoItem from './TodoItem.vue';
import TodoClearButton from './TodoClearButton.vue';
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const todos = ref([]);

const remainingTodos = computed(() => {
  return todos.value.length;
});

function addTodo(todo) {
  todos.value.push(todo);
}

function removeTodo(index) {
  todos.value.splice(index, 1);
}

function clearTodos() {
  todos.value = [];
}

function updateTodo({ index, newTodo }) {
  todos.value[index] = newTodo;
}

console.log('TodoList component is mounted');

const router = useRouter()
const authStore = useAuthStore()

const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.todo-container {
  font-family: Arial, sans-serif;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f5f7fa;
  padding: 10px 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* .nav-link {
  text-decoration: none;
  color: #333;
  font-weight: bold;
} */
.logout-btn {
  padding: 8px 16px;
}

el-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 60px);
}

p {
  margin-top: 20px;
  font-size: 18px;
}

.nav-link {
  text-decoration: none;
  color: #333;
  font-weight: bold;
}
</style>